package com.wnxy.admin.controller;


import com.wnxy.admin.common.util.AlyOssUtil;
import com.wnxy.admin.service.IChargingStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.UUID;

import static com.google.common.io.Files.getFileExtension;
@CrossOrigin
@RestController
@RequestMapping("/photo")
@Api(tags = "图片控制器")
public class PhotoController {

    private final IChargingStationService chargingStationServiceImpl;

    public PhotoController(IChargingStationService chargingStationServiceImpl) {
        this.chargingStationServiceImpl = chargingStationServiceImpl;
    }

    //文件上传
    @ApiOperation("文件上传")
    @PostMapping("/upload")

    public String upload(@RequestPart MultipartFile file) throws Exception{
//        if (file.isEmpty()){
//            return ResponseResult.fail(BusinessEnum.FILE_IS_EMPTY);
//        }

            // 在这里可以添加处理上传文件的逻辑，例如保存文件信息到数据库
            String uuid = String.valueOf(UUID.randomUUID());
            uuid = uuid.replace("-", "");
            String fileName = uuid + "." + getFileExtension(file.getOriginalFilename());

        // 上传文件到阿里云 OSS
            InputStream inputStream = file.getInputStream();
            String ossUrl = AlyOssUtil.uploadFile(fileName, inputStream);

            // 1. 获取文件的原名称
//            String oldFilename = file.getOriginalFilename();
//            // 2. 拼接新的名称
//            String type = oldFilename.substring(oldFilename.lastIndexOf("."));
//            String uuid = UUID.randomUUID().toString();
//            String newFilename = uuid + type;
//            // 3. 保存图片到本地磁盘地址
//            File local = new File("D:\\Temp\\" + newFilename);
//            file.transferTo(local);

//            ChargingStation chargingStation = new ChargingStation();
//            chargingStation.setChargingStationPhoto(ossUrl);
//            chargingStationServiceImpl.save(chargingStation);

//            return "http://localhost:9090/Temp/" + newFilename;
        return ossUrl;

    }
}
